-[ c library "trace.c", header "trace.h" ]

-[cbuffer_t : private buffer [200] of char]

-[public] void trace_print() {
	char *trace;
	int len;

	trace= cbuffer_collect(tbuf, &len);
	remove_balanced(trace,&len);
	fwrite(trace, 1,len, stdout);
}

#define LSQBR '{'
#define RSQBR '}'

-[private] void remove_balanced(char *trace,int *len)
{
	int L= *len;
again:
	for(i=0;i<L;i++) 
		if (trace[i]==LSQBR) {
			int E;
			E= end_of_brace(trace,i+1,L);
			if (E!=-1) {
				memmove(trace+i, trace+E, L-E);
				L-= E-i;
				i--;
			}
		}
	*len= L;
}

-[private] int end_of_brace(char *trace,int i,int L) {
	int Z;
	Z=1;
	for(;i<L;i++) 
		if (trace[i]==RSQBR) {
			Z--;
			if (!Z) return i+1;
		} else if trace[i]==LSQBR)
			Z++;
	return -1;
}

